package com.hyzh.latte.vehicle.nezha;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.coffee.concurrent.CoffeeExecutorManager;
import com.hyzh.latte.util.gather.OkhttpUtil;
import com.hyzh.latte.vehicle.dmo.entity.ParkPGEntity;
import com.hyzh.latte.vehicle.dmo.entity.VtypeEntity;
import com.hyzh.latte.vehicle.repository.VtypePGRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author songkui
 * @since 2024/7/6 15:47
 */
@Component
@Slf4j
@AllArgsConstructor
public class VehicleTypeScheduledTask {

    private final VtypePGRepository vtypePGRepository;

    @Scheduled(cron = "0 0 10,15 * * *")
    public void executeTask() {
        ParkDeviceBuilder builder = NezhaDeviceManager.getInstance().getBuilderData();
        List<String> parkList = builder.getParkMap();
        CoffeeExecutorManager.getInstance().getDefaultExecutor().execute(() -> {
            for (String park : parkList) {
                doTask(park);
            }
        });
    }

    private void doTask(String park) {
        ParkPGEntity parkEntity = NezhaPersonManager.getInstance().parkPGEntity(park);
        if (null == parkEntity || StrUtil.isEmpty(parkEntity.getVUrl())){
            log.error("VehicleTypeScheduledTask handler>> park  null; park: {}", park);
            return;
        }

        String url = parkEntity.getVUrl();
        List<Map<String, Object>> list = OkhttpUtil.pullVType(url);
        if (CollectionUtil.isEmpty(list)){
            log.error("VehicleTypeScheduledTask handler>> data isnull; park: {}; url: {}", park, url);
            return;
        }

        List<VtypeEntity> dbList = vtypePGRepository.list();
        List<VtypeEntity> entities = new ArrayList<>();
        for (Map<String, Object> map : list){
            String vno = (String) map.get("carNumb");
            String type = String.valueOf(Optional.ofNullable(map.get("type")).map(t -> Integer.parseInt(String.valueOf(t))).orElse(null));
            if (StrUtil.isEmpty(vno) || StrUtil.isEmpty(type)){
                continue;
            }
            if (!isExist(vno, dbList)){
                VtypeEntity vtypeEntity = new VtypeEntity();
                vtypeEntity.setType(type);
                vtypeEntity.setVno(vno);
                entities.add(vtypeEntity);
            }
        }

        if (CollectionUtil.isNotEmpty(entities)){
            vtypePGRepository.saveBatch(entities);
        }

    }

    private boolean isExist(String vno, List<VtypeEntity> list){
        if (CollectionUtil.isEmpty(list)){
            return false;
        }

        for (VtypeEntity entity : list){
            if (vno.equals(entity.getVno())){
                return true;
            }
        }
        return false;
    }



}

